package com.zygh.policeasset.service;

import cn.hutool.core.date.DateUtil;
import com.zygh.policeasset.component.ProjectCode;
import com.zygh.policeasset.mapper.ProcessRecordMapper;
import com.zygh.policeasset.mapper.ProjectApprovalStepsMapper;
import com.zygh.policeasset.mapper.ProjectMapper;
import com.zygh.policeasset.mapper.ProjectNoMapper;
import com.zygh.policeasset.pojo.ProcessRecord;
import com.zygh.policeasset.pojo.Project;
import com.zygh.policeasset.pojo.ProjectNo;
import java.util.List;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

@Service
/* loaded from: police-asset-2.0-0.0.1-SNAPSHOT.jar:BOOT-INF/classes/com/zygh/policeasset/service/ProcessRecordSerivce.class */
public class ProcessRecordSerivce {
    @Autowired
    private ProcessRecordMapper processRecordMapper;
    @Autowired
    ProjectCode projectCode;
    @Autowired
    ProjectMapper projectMapper;
    @Autowired
    ProjectNoMapper projectNoMapper;
    @Autowired
    ProjectApprovalStepsMapper projectApprovalStepsMapper;
    @Autowired
    TenderService tenderService;
    @Autowired
    ContractService contractService;
    @Autowired
    CompletedSerivce completedSerivce;
    @Autowired
    SettlementService settlementService;
    @Autowired
    FinalStatementService finalStatementService;

    public Integer add(ProcessRecord processRecord) {
        return Integer.valueOf(this.processRecordMapper.add(processRecord));
    }

    public List<ProcessRecord> findAll(int id) {
        return this.processRecordMapper.findAll(id);
    }

    public Integer updateStatus(ProcessRecord params) {
        return this.processRecordMapper.updateStatus(params);
    }

    public ProcessRecord findOne(ProcessRecord params) {
        return this.processRecordMapper.findOne(params);
    }

    public List<ProcessRecord> findByProjectId(Integer projectId, String processName) {
        return this.processRecordMapper.findByProjectId(projectId, processName);
    }

    public Integer submit(ProcessRecord processRecord) {
        int res = 0;
        try {
            processRecord.setProcessTime(DateUtil.now());
            processRecord.setStatus(2);
            res = this.processRecordMapper.add(processRecord);
            if (res > 0 && processRecord.getProcessName().equals("project")) {
                Project project = this.projectMapper.findById(processRecord.getProjectId());
                String year = project.getStartDatetime().substring(0, 4);
                ProjectNo projectNo = this.projectNoMapper.findByType(project.getType(), year);
                if (projectNo == null) {
                    projectNo = new ProjectNo();
                    projectNo.setType(project.getType());
                    projectNo.setCounter(1);
                    projectNo.setYear(year);
                    this.projectNoMapper.add(projectNo);
                }
                String no = this.projectCode.get(project.getType(), String.valueOf(projectNo.getCounter()), year);
                res = this.projectNoMapper.update(project.getType(), year).intValue();
                if (this.projectMapper.findByNo(no) == null) {
                    this.projectMapper.updateProjectNo(project.getId().intValue(), no);
                } else {
                    this.projectMapper.updateProjectNo(project.getId().intValue(), this.projectCode.get(project.getType(), String.valueOf(projectNo.getCounter() + 1), year));
                }
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        return Integer.valueOf(res);
    }

    public Integer submitbenji(ProcessRecord processRecord) {
        int res = 0;
        try {
            int count = this.projectApprovalStepsMapper.findByid(processRecord.getProcessName(), 1);
            System.out.println(processRecord);
            if ((count > 0) && (processRecord.getStatus() == 1)) {
                processRecord.setProcessTime(DateUtil.now());
                processRecord.setStatus(1);
                processRecord.setOpinion(processRecord.getOpinion());
                res = this.processRecordMapper.add(processRecord);
                updatestatus(processRecord.getProcessName(), Integer.valueOf(processRecord.getProjectId()), 1);
            } else if (processRecord.getStatus() == 0) {
                processRecord.setProcessTime(DateUtil.now());
                processRecord.setStatus(0);
                processRecord.setOpinion(processRecord.getOpinion());
                res = this.processRecordMapper.add(processRecord);
                updatestatus(processRecord.getProcessName(), Integer.valueOf(processRecord.getProjectId()), 0);
            } else if (processRecord.getStatus() == 3) {
                processRecord.setProcessTime(DateUtil.now());
                processRecord.setStatus(3);
                processRecord.setOpinion(processRecord.getOpinion());
                res = this.processRecordMapper.add(processRecord);
                updatestatus(processRecord.getProcessName(), Integer.valueOf(processRecord.getProjectId()), 3);
            } else {
                processRecord.setProcessTime(DateUtil.now());
                processRecord.setStatus(2);
                processRecord.setOpinion(processRecord.getOpinion());
                res = this.processRecordMapper.add(processRecord);
                if (res > 0 && processRecord.getProcessName().equals("project")) {
                    Project project = this.projectMapper.findById(processRecord.getProjectId());
                    String year = project.getStartDatetime().substring(0, 4);
                    ProjectNo projectNo = this.projectNoMapper.findByType(project.getType(), year);
                    if (projectNo == null) {
                        projectNo = new ProjectNo();
                        projectNo.setType(project.getType());
                        projectNo.setCounter(1);
                        projectNo.setYear(year);
                        this.projectNoMapper.add(projectNo);
                    }
                    String no = this.projectCode.get(project.getType(), String.valueOf(projectNo.getCounter()), year);
                    res = this.projectNoMapper.update(project.getType(), year).intValue();
                    if (this.projectMapper.findByNo(no) == null) {
                        this.projectMapper.updateProjectNo(project.getId().intValue(), no);
                    } else {
                        this.projectMapper.updateProjectNo(project.getId().intValue(), this.projectCode.get(project.getType(), String.valueOf(projectNo.getCounter() + 1), year));
                    }
                }
                updatestatus(processRecord.getProcessName(), Integer.valueOf(processRecord.getProjectId()), 2);
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        return Integer.valueOf(res);
    }

    public void updatestatus(String name, Integer id, Integer status) {
        if (name.equals("project")) {
            this.projectMapper.updateStatus(id.intValue(), status.intValue());
        } else if (name.equals("tender")) {
            this.tenderService.updateStatus(id.intValue(), status.intValue());
        } else if (name.equals("contract")) {
            this.contractService.updateStatus(id.intValue(), status.intValue());
        } else if (name.equals("completed")) {
            this.completedSerivce.updateStatus(id.intValue(), status.intValue());
        } else if (name.equals("settlement")) {
            this.settlementService.updateStatus(id.intValue(), status.intValue());
        } else if (name.equals("final_statement")) {
            this.finalStatementService.updateStatus(id.intValue(), status.intValue());
        }
    }
}